package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityTalentWithdrawRecordBinding
import com.library.common.base.BaseActivity
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.dto.BillListDto
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.TalentWalletViewModel
import kotlinx.android.synthetic.main.activity_bill.*

class TalentWithdrawRecordActivity :
    BaseActivity<TalentWalletViewModel, ActivityTalentWithdrawRecordBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_talent_withdraw_record

    private var pageNumber = 1
    private var type: String? = ""

    override fun onGetBundle(bundle: Bundle?) {
        type = bundle?.getString("type")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(
            if (type == getString(R.string.expense))
                getString(R.string.bill)
            else
                getString(R.string.income_record_1)
        )
        mBinding.mViewModel = viewModel
    }

    override fun observeData() {
        viewModel.getBillList(type, pageNumber)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getBillList(type, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getBillList(type, pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getBillList(type, pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                IncomeRecordDetailActivity.openActivity(mContext,(msg.obj as BillListDto).id)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, type: String) {
            mContext.startActivity(
                bundleOf(Pair("type", type)),
                TalentWithdrawRecordActivity::class.java
            )
        }
    }
}
